package Adaptadores;

import java.util.ArrayList;


import Persistencia.CreadorBD;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;

import android.widget.TextView;

import entidades.Log;
import entidades.ShellSort;

public class AdaptadorListaLog extends BaseAdapter {
	ArrayList<Log> listaLog;
	private LayoutInflater inflater;
	private Log logActual;
	private CreadorBD base;
	private Context context;

	public AdaptadorListaLog(Context context) {

		inflater = LayoutInflater.from(context);
		this.context = context;
		base = new CreadorBD(this.context);

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			convertView = inflater.inflate(
					com.example.tdam_2012_g3.R.layout.lista_log_item, null);
			holder = new Holder();
			holder.txtName = (TextView) convertView.findViewById(com.example.tdam_2012_g3.R.id.txtNombreContactoLog);
			holder.txtFecha = (TextView) convertView.findViewById(com.example.tdam_2012_g3.R.id.txtFechaLog);
			holder.txtHora = (TextView) convertView.findViewById(com.example.tdam_2012_g3.R.id.txtHoraLog);
			holder.imgTipo = (ImageView) convertView.findViewById(com.example.tdam_2012_g3.R.id.imgImagenLog);
			convertView.setTag(holder);
		}
		else {

			holder = (Holder) convertView.getTag();

		}

		logActual = (Log) this.getItem(position);
		if (logActual != null) {
			holder.txtName.setText(logActual.getUsuario());
			holder.txtFecha.setText(logActual.getFecha());
			holder.txtHora.setText(logActual.getHora());
			if (logActual.getAccion().toUpperCase().equals("L")) {

				holder.imgTipo.setImageResource(android.R.drawable.sym_call_outgoing);
			}

			else if (logActual.getAccion().toUpperCase().equals("M")) {
				holder.imgTipo.setImageResource(android.R.drawable.sym_action_email);
			} else if (logActual.getAccion().toUpperCase().equals("E")) {
				holder.imgTipo.setImageResource(com.example.tdam_2012_g3.R.drawable.ic_email);
			} else if (logActual.getAccion().toUpperCase().equals("S")) {
				holder.imgTipo.setImageResource(com.example.tdam_2012_g3.R.drawable.ic_sms_web_service);
			}

		}

		return convertView;

	}

	class Holder {
		private TextView txtName;
		private TextView txtFecha;
		private TextView txtHora;
		private ImageView imgTipo;
	}

	public int getCount() {
		return listaLog.size();
	}

	public Object getItem(int position) {
		return listaLog.get(position);
	}

	public long getItemId(int position) {
		return listaLog.get(position).getId();
	}

	public void agregarContacto(Log l) {
		if (l != null)
			this.listaLog.add(l);
	}

	public void setListaDeAcciones(ArrayList<Log> lista) {
		this.listaLog = lista;
	}

	public void sortAccion(boolean ascendente, String criterio) {
		ShellSort sort = new ShellSort();
		if (criterio.equals(ShellSort.CRITERIO_CONTACTO)) {
			if (ascendente) {
				listaLog = sort.doSortUserAscending(listaLog);

			} else {
				listaLog = sort.doSortUserDescending(listaLog);
			}
		}

		else {
			if (ascendente) {
				listaLog = sort.doSortDateAscending(listaLog);

			} else {
				listaLog = sort.doSortDateDescending(listaLog);
			}

		}

	}

}
